package com.codeark.appengine;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import com.google.common.io.InputSupplier;
import com.google.inject.Inject;

/**
 * 
 * @author Yaniv Kessler (yaniv at codeark dot com)
 *
 */
public class StringUrlFetchServiceFacade extends AbstractUrlFetchServiceFacade<String> {

	@Inject
	public StringUrlFetchServiceFacade(URLFetchService web) {
		super(web);
	}

	@Override
	protected String convertResponse(HTTPResponse response) throws IOException {
		InputSupplier<ByteArrayInputStream> iss = ByteStreams.newInputStreamSupplier(response.getContent());
		InputSupplier<InputStreamReader> reader = CharStreams.newReaderSupplier(iss, Charset.forName("utf8"));
		return CharStreams.toString(reader);
	}
}
